// pages/smartlife/cheap/cheap.js
var common = require("../../../utils/common.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cheaps: [
      // { "title": "欢乐颂", "img": "http://xiaozhi.iwares.cn:8080/wangcai/images/huanlesong_small.jpg", "link": "/pages/smartlife/common_recommend/common_recommend?recommendid=2&title=欢乐颂全集&img=http://xiaozhi.iwares.cn:8080/wangcai/images/huanlesong_big.jpg&content=点击下面的分享按钮，并成功分享到任意群即可收到链接的推送消息" }, { "title": "广告招商", "img": "http://xiaozhi.iwares.cn:8080/wangcai/images/business.png", "link": "13913213202" }, { "title": "", "img": "http://xiaozhi.iwares.cn:8080/wangcai/images/postalinsurance.jpg", "link": "/pages/smartlife/post_auto_insurance/post_auto_insurance" }
    ],
    recommendImgWidth: 0,
    page:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var appUsed = wx.getStorageSync('appused')
    if (!appUsed) {
      // 跳转到app使用引导页面
      wx.navigateTo({
        url: '/pages/smartlife/guide/guide'
      })
      wx.setStorageSync('appused', true)
    }
    try {
      var res = wx.getSystemInfoSync()
      that.setData({
        recommendImgWidth: res.windowWidth - 20
      })
    } catch (e) {
      // Do something when catch error
    }
    that.onPullDownRefresh()
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '优惠活动尽在鹿城小智',
      path: '/pages/smartlife/cheap/cheap'
    }
  },
  onPullDownRefresh: function () {
    var that = this
    that.setData({
      page: 1
    })
    that.getCheaps(false)
  },
  onReachBottom: function () {
    var that = this
    that.getCheaps(true)
  },
  getCheaps: function (isMore) {
    // 请求获取recommends列表
    var that = this
    wx.request({
      url: common.apiUrl() + 'cheapList?page='+that.data.page,
      success: function (res) {
        if (res.data.status == 1) {
          // console.log(res.data.data)
          if (isMore) {
            that.setData({
              cheaps: that.data.cheaps.concat(res.data.data),
              page: that.data.page+1
            })
          } else {
            that.setData({
              cheaps: res.data.data,
              page: that.data.page+1
            })
            wx.stopPullDownRefresh()
          }
        }
      }
    })
  },
  recommendClick: function (e) {
    var that = this
    if (isNaN(e.currentTarget.dataset.link)) {
      if (e.currentTarget.dataset.link) {
        var page = e.currentTarget.dataset.link + '?recommendid=' + that.data.cheaps[e.currentTarget.dataset.index].id + '&title=' + that.data.cheaps[e.currentTarget.dataset.index].title + '&img=' + that.data.cheaps[e.currentTarget.dataset.index].img + '&content=' + that.data.cheaps[e.currentTarget.dataset.index].content + '&hiddendel=false'
        // console.log(page)
        wx.navigateTo({
          url: page,
          success: function (res) {
            // success
          },
          fail: function () {
            // fail
          },
          complete: function () {
            // complete
          }
        })
      } else {
        // wx.showToast({
        //   title: '暂未开放'
        // })
      }
    } else {
      if (e.currentTarget.dataset.link == null || e.currentTarget.dataset.link == undefined || e.currentTarget.dataset.link == '') {
        // console.log(e.currentTarget.dataset.link)
      } else {
        wx.makePhoneCall({
          phoneNumber: e.currentTarget.dataset.link,
        })
      }
    }
  },
  moveStart: function (e) {
    var that = this
    that.setData({
      addHidden: true
    })
  },
  moveEnd: function (e) {
    var that = this
    that.setData({
      addHidden: false
    })
  },
  addClick: function (e) {
    wx.navigateTo({
      url: '/pages/mine/recommend_upload/recommend_upload',
    })
  }
})